package com.example.fpe_vote.db1.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fpe_vote.db1.entity.NomineeInfo;
import com.example.fpe_vote.db1.entity.PrizeInfo;
import com.example.fpe_vote.db1.entity.VoteInfo;
import com.example.fpe_vote.db1.mapper.VoteInfoMapper;
import com.example.fpe_vote.db1.service.INomineeInfoService;
import com.example.fpe_vote.db1.service.IPrizeInfoService;
import com.example.fpe_vote.db1.service.IVoteInfoService;
import com.example.fpe_vote.utils.MyStringUtil;
import com.example.fpe_vote.vo.request.RequestAddVoteVo;
import com.example.fpe_vote.vo.request.RequestListVoteInfoVo;
import com.example.fpe_vote.vo.response.ResponseListVoteInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2022-12-01
 */
@Service
public class VoteInfoServiceImpl extends ServiceImpl<VoteInfoMapper, VoteInfo> implements IVoteInfoService {
    @Autowired
    INomineeInfoService nomineeInfoService;
    @Autowired
    IPrizeInfoService prizeInfoService;

    @Override
    public VoteInfo queryOne(String userName, String ghid, String prizeNum) {
        QueryWrapper<VoteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name", userName);
        queryWrapper.eq("ghid", ghid);
        queryWrapper.eq("prize_num", prizeNum);
        List<VoteInfo> list=list(queryWrapper);
        if(list==null||list.size()==0){
            return null;
        }else
            return list(queryWrapper).get(0);
    }

    @Override
    public Boolean addVote(RequestAddVoteVo voteVo) {
        VoteInfo voteInfo=new VoteInfo();
        voteInfo.setGhid(voteVo.getGhid());
        voteInfo.setName(voteVo.getUserName());
        voteInfo.setPrizeNum(voteVo.getPrizeNum());
        voteInfo.setVoteNomineeId(MyStringUtil.strToString(voteVo.getVoteNomineeIds()));
        String[] ids=voteVo.getVoteNomineeIds();
        String[] names=new String[voteVo.getVoteNomineeIds().length];
        for(int i=0;i<ids.length;i++){
            NomineeInfo nomineeInfo=nomineeInfoService.getById(Integer.parseInt(ids[i]));
            names[i]=nomineeInfo.getName();
        }
        voteInfo.setVoteNomineeName(MyStringUtil.strToString(names));
        PrizeInfo prizeInfo=prizeInfoService.queryByNum(voteVo.getPrizeNum());
        if(ObjectUtils.isNotEmpty(prizeInfo)){
            voteInfo.setPrizeName(prizeInfo.getPrizeName());
        }else
            voteInfo.setPrizeName("未知");
        if(save(voteInfo)){
            //执行票数增加
            nomineeInfoService.addVotes(ids);
            return true;
        }else
            return false;
    }

    @Override
    public IPage<ResponseListVoteInfoVo> listVoteInfo(RequestListVoteInfoVo voteInfoVo) {
        IPage<VoteInfo> iPage=new Page<>(voteInfoVo.getDisplayStart(),voteInfoVo.getDisplayLength());
        QueryWrapper<VoteInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",voteInfoVo.getUserName());
        queryWrapper.eq("ghid",voteInfoVo.getGhid());
        if(ObjectUtils.isNotEmpty(voteInfoVo.getPrizeName())){
            queryWrapper.like("prize_name",voteInfoVo.getPrizeName());
        }
        int allTotal=list(queryWrapper).size();
        IPage<VoteInfo> page=page(iPage,queryWrapper);
        return page.convert(voteInfo -> {
            ResponseListVoteInfoVo vo=new ResponseListVoteInfoVo();
            vo.setCreateTime(voteInfo.getCreateTime());
            vo.setPrizeName(voteInfo.getPrizeName());
            vo.setVoteNomineeName(voteInfo.getVoteNomineeName());
            PrizeInfo prizeInfo=prizeInfoService.queryByNum(voteInfo.getPrizeNum());
            vo.setStatus(prizeInfo.getStatus());
            vo.setTotal(allTotal);
            return vo;
        });
    }
}
